package com.Mexico;

import java.io.IOException;
import javax.servlet.http.*;

import java.sql.*;
import com.google.appengine.api.rdbms.AppEngineDriver;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;



@SuppressWarnings("serial")
public class MexicoServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		
		
					   
					  PrintWriter out = resp.getWriter();
					  Connection c = null;
					    try {
					      DriverManager.registerDriver(new AppEngineDriver());
					      c = DriverManager.getConnection("jdbc:google:rdbms://tomasestasaqui:tomasesta/musica");
					      String nombre = req.getParameter("nombre");
					      String disco = req.getParameter("disco");
					      String cancion = req.getParameter("cancion");
					      if (nombre == "" || disco == ""||cancion=="") {
					        out.println("<html><head></head><body>You are missing either a message or a name! Try again! Redirecting in 3 seconds...</body></html>");
					      } else {
					      String statement ="INSERT INTO artista (guestName, content) VALUES( 'La ley' , 'mentiras','mentiras' )";
					      PreparedStatement stmt = c.prepareStatement(statement);
					      stmt.setString(1, nombre);
					      stmt.setString(2, disco);
					      stmt.setString(3, cancion);
					      int success = 2;
					      //success = stmt.executeUpdate();
					      if(success == 1) {
					        out.println("<html><head></head><body>Success! Redirecting in 3 seconds...</body></html>");
					      } else if (success == 0) {
					        out.println("<html><head></head><body>Failure! Please try again! Redirecting in 3 seconds...</body></html>");
					      }
					     }
					    } catch (SQLException e) {
					        e.printStackTrace();
					    } finally {
					        if (c != null) 
					          try {
					            c.close();
					            } catch (SQLException ignore) {
					         }
					      } resp.setHeader("Refresh","3; url=/musica.jsp");
					  }
	
		
	
}
